const app = getApp()

Page({
  data: {
    telPhone: '',
    loading: false,
    btnText: true,
    disabled: true,
    VerCode: '',
    hasTel: true,
    hasTelDisabled: true,
    second: 30
  },
  onShow: function () {
    var _this = this;
    wx.getStorage({
      key: "telPhone",
      success: function (res) {
        console.log(res.data)
        if (res.data) {
          _this.setData({
            nickName: res.data.telPhone
          })
        }
        console.log(_this.data.telPhone)
      }
    })
    wx.setNavigationBarTitle({
      title: '更改手机号'
    })
  },
  onLoad: function () {
    countdown(this);
  },

  getTelPhone: function (e) {
    var telPhone = e.detail.value;
    this.setData({
      telPhone: e.detail.value
    })
    if (telPhone.length == 11) {
      this.setData({
        disabled: false
      })
    }
      
  },
  getVerCode: function(e) {
    this.setData({
      VerCode: e.detail.value
    })
  },
  // 请求验证码
  yzTel: function() {
    var telPhone = this.data.telPhone;
    var VerCode = this.data.VerCode;
    var time = 30;
    
    app.telephone(telPhone)  
    if (app.telephone(telPhone)) {  // 返回true
      app.countdown
      this.setData({
        btnText: false,
      })
    }
  },
  // 验证码请求成功  点击保存
  submit: function (e) {
    var telPhone = this.data.telPhone;
     var VerCode = this.data.VerCode;
    // console.log("VerCode", VerCode);
    // console.log("telPhone", telPhone);
    if (!telPhone) {
      wx.showToast({
        title: '请填写手机号',
        duration: 1000,
        mask: true
      })
    } else if (telPhone.length != 11) {
      wx.showToast({
        title: '手机号不正确',
        duration: 1000,
        mask: true
      })
    } else if (!VerCode) {
      // console.log('请填写验证码')
      wx.showToast({
        title: '请填写验证码',
        duration: 1000,
        mask: true
      })
    } else {
      this.setData({ loading: true })
      wx.setStorage({
        key: "telPhone",
        data: telPhone
      })
      wx.navigateBack({
        delta: 1
      })
    }
    

  },
  getPhoneNumber: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    var _this = this;
    if (e.detail.errMsg == 'getPhoneNumber:fail 该 appid 没有权限') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) {
          console.log(res)
         }
      })
      _this.setData({
        hasTel: false
      })

    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '同意授权',
        success: function (res) {
          console.log(res)
         }
      })
      _this.setData({
        hasTelDisabled: false
      })
    }
  } 
})